package com.sc.nft.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.sc.nft.annotation.UserLogin;
import com.sc.nft.controller.request.*;
import com.sc.nft.controller.vo.TransferFeeVO;
import com.sc.nft.dao.*;
import com.sc.nft.entity.*;
import com.sc.nft.entity.congestive.GiftCT;
import com.sc.nft.entity.dto.*;
import com.sc.nft.entity.vo.*;
import com.sc.nft.entity.vo.metaWalletDTO.UserGetGiveMetaWalleFeeVO;
import com.sc.nft.enums.RedisKeyEnum;
import com.sc.nft.enums.SendSMSEnum;
import com.sc.nft.enums.UserAccoutTypeEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.pool.UserPool;
import com.sc.nft.service.CheckParameterService;
import com.sc.nft.service.CollectionGiftService;
import com.sc.nft.service.MetaWalletService;
import com.sc.nft.service.UserCollectionService;
import com.sc.nft.sup.ErrorCode;
import com.sc.nft.sup.PageRequest;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import com.tencentcloudapi.cam.v20190116.models.AttachedUserPolicy;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * 服务控制器
 *
 * @author Sun
 * @description 由 Mybatisplus Code Generator 创建
 * @since 2022-05-29 10:36:55
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/collection")
@Api(tags = "用户藏品表")
public class UserCollectionController {

    private final UserCollectionService userCollectionService;

    private final CheckParameterService checkParameterService;

    private final StaticDataDao staticDataDao;

    private final PrivilegeDao privilegeDao;

    private final FilterIncomeListDao filterIncomeListDao;

    private final Redisson redisson;

    private final UserCollectionDao userCollectionDao;
    private final MetaWalletService metaWalletService;
    private final CollectionGiftService collectionGiftService;

    @ApiOperation(value = "我的藏品", notes = "我的藏品", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/myCollectionNow", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<MyCollectionNowVO> getUserInvitePageNow(@RequestBody MyCollectionRequest request) {
        return PageResult.success(userCollectionService.myCollectionNow(request.getIsPrivilege(), UserPool.getUser().getId(), request.getPageNo(), request.getPageSize()));
    }

    @ApiOperation(value = "我的藏品(老版本)", notes = "我的藏品(老版本)", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/myCollection", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<MyCollectionVO> getUserInvitePage(@RequestBody MyCollectionRequest request) {
        return PageResult.success(userCollectionService.myCollection(request.getIsPrivilege(), UserPool.getUser().getId(), request.getPageNo(), request.getPageSize()));
    }

    @ApiOperation(value = "我的藏品列表", notes = "我的藏品列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/myCollectionListByCollectionId", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<MyCollectionListByCollectionIdVO> myCollectionListByCollectionId(@RequestBody MyCollectionListByCollectionIdRequest request) {
        Page<MyCollectionListByCollectionIdVO> page = userCollectionService.myCollectionListByCollectionId(request.getCollectionId(), UserPool.getUser().getId(), request.getPageNo(), request.getPageSize());
        return PageResult.success(page);
    }

    @ApiOperation(value = "我的盲盒", notes = "我的盲盒", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/myBox", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<MyBoxVO> myBox(@Valid @RequestBody QueryMyBoxRequest request) {
        return PageResult.success(userCollectionService.myBox(UserPool.getUser().getId(), request.getGiveStatus(), request.getPageNo(), request.getPageSize()));
    }

    @ApiOperation(value = "转赠人脸核验", notes = "转赠人脸核验", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/donationFaceVerification", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<DonationFaceVO> donationFaceVerification(@Valid @RequestBody DonationFaceVerificationRequest request) throws IOException {
        DonationFaceVO donationFaceVO = userCollectionService.donationFaceVerification(request.getUrl());
        return Result.success(donationFaceVO);
    }

    @ApiOperation(value = "转增", notes = "转增", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/give", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result give(@Valid @RequestBody GiveCollectionRequest request) {
        if (true) {
            throw new GlobalRunTimeException("该版本转赠功能已经关闭,请升级至最新版本");
        }
        RLock lock = redisson.getLock(RedisKeyEnum.TRANSFER_LOCK.getKey() + request.getId());
        try {
            boolean res = lock.tryLock(3,  TimeUnit.SECONDS);
            if (res) {
                UserInfo user = UserPool.getUser();
                String userAccout;
                if (user.getIsUser() == UserAccoutTypeEnum.USER_TEL.getCode()) {
                    userAccout = user.getUserTel();
                } else {
                    userAccout = user.getUserEmail();
                }
                FilterIncomeList isTeamIncomeByUserId = filterIncomeListDao.getIsTeamIncomeByUserId(user.getId());
                if (ObjectUtil.isNull(isTeamIncomeByUserId)) {
                    checkParameterService.CheckVerificationcode(userAccout, request.getSmsId(), request.getCodes(), SendSMSEnum.TRANSFER.getCode());
                } else {
                    if (!StrUtil.equals(user.getUserTel().substring(user.getUserTel().length() - 6), request.getCodes())) {
                        throw new GlobalRunTimeException(ErrorCode.VERIFICATION_ERROR);
                    } else {
                        user.setIsValid(true);//市值假装实名过
                    }
                }
                userCollectionService.give(UserPool.getUser(), request.getToUserUUID(), request.getId());
            }else {
                return Result.fail(ErrorCode.LATER_TRY);
            }
        } catch (InterruptedException e) {
            throw new GlobalRunTimeException("系统繁忙，请稍后再试");
        } finally {
            lock.unlock();
        }

        return Result.success();
    }
    @ApiOperation(value = "跨链转增", notes = "跨链转增", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/giveMetaWallet", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result giveMetaWallet(@Valid @RequestBody GiveMetaWalletRequest request) {

        UserInfo user = UserPool.getUser();
        Assert.isTrue(user.getTransactionPassword().equals(request.getTransactionPassword()), () -> new GlobalRunTimeException("交易密码错误"));

        List<RLock>locks=new ArrayList<>();
        try{
            for (Long userCollectionId : request.getUserCollectionIds()) {
                String key=StrUtil.format("{}:{}", RedisKeyEnum.TRANSFER_LOCK.getKey(),userCollectionId);
                RLock lock = redisson.getLock(key);
                locks.add(lock);
                boolean res = lock.tryLock(10, TimeUnit.SECONDS);
                if (!res) {
                    return Result.fail("请勿重复提交");
                }
            }
            userCollectionService.giveMetaWallet(request.getUserAddr(),request.getConsumeType(),request.getCollectionId(),request.getUserCollectionIds(),user);
        }catch (InterruptedException e) {
            return Result.fail("赠送失败");
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } finally {
            locks.forEach(rLock -> {
                if (rLock.isLocked()) {
                    rLock.unlock();
                }
            });
        }
        return Result.success();
    }


    @ApiOperation(value = "藏品转增", notes = "藏品转增", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/collectionGive", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result collectionGive(@Valid @RequestBody CollectionGiveDTO request) {
        RLock lock = redisson.getLock(RedisKeyEnum.TRANSFER_LOCK.getKey() + request.getId());
        try {
            boolean res = lock.tryLock(3,  TimeUnit.SECONDS);
            if (res) {
                request.setUserInfo(UserPool.getUser());
            userCollectionService.collectionGive(request);
            }else {
                return Result.fail(ErrorCode.LATER_TRY);
            }
        } catch (InterruptedException e) {
            throw new GlobalRunTimeException("系统繁忙，请稍后再试");
        } finally {
            if (lock.isLocked()) {
                lock.unlock();
            }
        }
        return Result.success();
    }


    @ApiOperation(value = "藏品批量转增", notes = "藏品批量转增", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/collectionBatchGive", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result collectionBatchGive(@Valid @RequestBody CollectionBatchGiveDTO request) {
        List<RLock>locks=new ArrayList<>();
        List<Long> collectionIdList = request.getCollectionIdList();
        request.setUserInfo(UserPool.getUser());
        try{
            for (Long id : collectionIdList) {
                RLock lock = redisson.getLock(RedisKeyEnum.TRANSFER_LOCK.getKey() + id);
                locks.add(lock);
                boolean res = lock.tryLock(10, TimeUnit.SECONDS);
                if (!res) {
                    return Result.fail("请勿重复提交");
                }
            }
            userCollectionService.collectionBatchGive(request);
        }catch (InterruptedException e) {
            return Result.fail("赠送失败");
        }finally {
            locks.forEach(rLock -> {
                if (rLock.isLocked()) {
                    rLock.unlock();
                }
            });
        }
        return Result.success();

    }

    @ApiOperation(value = "转增详情", notes = "转增详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/giveDetails", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result giveDetails(@Valid @RequestBody GiveDetailsRequest request) {
        return Result.success(userCollectionService.giveDetails(UserPool.getUser(), request.getId()));
    }

    @ApiOperation(value = "批量转增到钱包详情", notes = "批量转增到钱包详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/batchGiveMetaWalletDetails", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<BatchGiveMetaWalletDetailsVO> batchGiveMetaWalletDetails(@Valid @RequestBody GiveDetailsRequest request) {
        return Result.success(userCollectionService.batchGiveMetaWalletDetails(UserPool.getUser(), request.getId()));
    }


    @ApiOperation(value = "获取转增手续费列表", notes = "获取转增手续费列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/getGiveMetaWalleFee", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<UserGetGiveMetaWalleFeeVO> getGiveMetaWalleFee(@Valid @RequestBody GetGiveMetaWalleFeeRequest request)  {
        UserGetGiveMetaWalleFeeVO page;

        try{
             page = collectionGiftService.getGiveMetaWalleFee(UserPool.getUser(), request.getUserAddr(), request.getCollectionId(), request.getUserCollectionIds());
        }catch (Exception e){
            log.error(e.getMessage());
            return Result.fail("系统繁忙,请稍后重试");
        }
        return Result.success(page);
    }


    @ApiOperation(value = "转赠损耗计算", notes = "转赠损耗计算", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/incrementalLossCalculation", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<List<IncrementalLossCalculationVO>> incrementalLossCalculation(@Valid @RequestBody CollectionIncrementalLossCalculationDTO incrementalLossCalculationDTO) {
        return Result.success(collectionGiftService.incrementalLossCalculation(incrementalLossCalculationDTO, UserPool.getUser()));
    }

    @ApiOperation(value = "转赠所需积分", notes = "转增所需积分", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/giveRequiredIntegral", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<GiveRequiredIntegralVO> giveRequiredIntegral(@Valid @RequestBody GiveRequiredIntegralDTO giveRequiredIntegralDTO) {
        return Result.success(userCollectionService.giveRequiredIntegral(UserPool.getUser(), giveRequiredIntegralDTO));
    }
    @ApiOperation(value = "获取转增手续费", notes = "获取转增手续费", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/getGiveFee", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result getGiveFee() {
        TransferFeeVO transferFeeVO = new TransferFeeVO();
        BigDecimal giveFee = new BigDecimal(staticDataDao.getByType("give_fee").getValue());
        transferFeeVO.setOriginalFee(giveFee);
        transferFeeVO.setFee(giveFee);
        UserInfo userInfo = UserPool.getUser();
        if (userInfo.getPrivilegeLevel() != null && userInfo.getPrivilegeLevel() > 0) {
            Privilege privilege = privilegeDao.getByLevel(userInfo.getPrivilegeLevel());
            if (privilege.getTransferDiscount().compareTo(BigDecimal.ZERO) > 0) {
                transferFeeVO.setDiscount(privilege.getTransferDiscount());
                transferFeeVO.setFee(giveFee.multiply(privilege.getTransferDiscount()));
            }
        }
        return Result.success(transferFeeVO);
    }

    @ApiOperation(value = "打开盲盒", notes = "打开盲盒", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/openBox", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<MyCollectionVO> openBox(@Valid @RequestBody IdRequest request) {
        UserCollection userCollectionPram = userCollectionDao.getLastNotOpenBox(UserPool.getUser().getId(), request.getId());
        Assert.notNull(userCollectionPram, () -> new GlobalRunTimeException("当前没有未开启的盲盒"));

        RLock lock = redisson.getLock(RedisKeyEnum.OPEN_BOX_LOCK.getKey() + userCollectionPram.getCollectionId());
        UserCollection userCollection = new UserCollection();
        try {
            boolean res = lock.tryLock(3,  TimeUnit.SECONDS);
            if (res) {
                userCollection = userCollectionService.openBox(UserPool.getUser(), request.getId());
            }else {
                return Result.fail(ErrorCode.LATER_TRY);
            }
        } catch (InterruptedException e) {
            throw new GlobalRunTimeException("系统繁忙，请稍后再试");
        } finally {
            lock.unlock();
        }

        return Result.success(userCollection, MyCollectionVO.class);
    }

    @ApiOperation(value = "转赠记录", notes = "转赠记录", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/giveCollectionLog", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<CollectionLogVO> giveCollectionLog(@RequestBody PageRequest request) {

        Page<CollectionLogDTO> page = userCollectionService.giveCollectionLog(UserPool.getUser(), request.getPageNo(), request.getPageSize());
        Page<CollectionLogVO> resultPage = new Page<>(page.getCurrent(), page.getSize(), page.getTotal());
        List<CollectionLogVO> vos = Lists.newArrayList();
        page.getRecords().forEach(collectionLogDTO -> {
            CollectionLogVO collectionLogVO = new CollectionLogVO();
            collectionLogVO.setId(collectionLogDTO.getId());
            if (collectionLogDTO.getIsOpen()) {
                collectionLogVO.setCollectionImage(collectionLogDTO.getCollectionImage());
                collectionLogVO.setFirstTitle(collectionLogDTO.getFirstTitle());
            } else {
                collectionLogVO.setCollectionImage(collectionLogDTO.getBoxImage());
                collectionLogVO.setFirstTitle(collectionLogDTO.getBoxTitle());
            }
            collectionLogVO.setGiveStatus(collectionLogDTO.getGiveStatus());
            collectionLogVO.setUserCreatorId(collectionLogDTO.getUserCreatorId());
            collectionLogVO.setUserCreatorImg(collectionLogDTO.getUserCreatorImg());
            collectionLogVO.setUserCreatorName(collectionLogDTO.getUserCreatorName());
            collectionLogVO.setToUserName(collectionLogDTO.getUserName());
            collectionLogVO.setGiveTime(collectionLogDTO.getGiveTime());
            collectionLogVO.setGiveSuccessTime(collectionLogDTO.getGiveSuccessTime());
            vos.add(collectionLogVO);
        });
        resultPage.setRecords(vos);
        return PageResult.success(resultPage);
    }

    @ApiOperation(value = "收到转增记录", notes = "收到转增记录", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/getCollectionLog", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<MyCollectionVO> getCollectionLog(@RequestBody PageRequest request) {
        Page<CollectionLogDTO> page = userCollectionService.getCollectionLog(UserPool.getUser(), request.getPageNo(), request.getPageSize());
        Page<CollectionLogVO> resultPage = new Page<>(page.getCurrent(), page.getSize(), page.getTotal());
        List<CollectionLogVO> vos = Lists.newArrayList();
        page.getRecords().forEach(collectionLogDTO -> {
            CollectionLogVO collectionLogVO = new CollectionLogVO();
            collectionLogVO.setId(collectionLogDTO.getId());
            if (collectionLogDTO.getIsOpen()) {
                collectionLogVO.setCollectionImage(collectionLogDTO.getCollectionImage());
                collectionLogVO.setFirstTitle(collectionLogDTO.getFirstTitle());
            } else {
                collectionLogVO.setCollectionImage(collectionLogDTO.getBoxImage());
                collectionLogVO.setFirstTitle(collectionLogDTO.getBoxTitle());
            }
            collectionLogVO.setGiveStatus(collectionLogDTO.getGiveStatus());
            collectionLogVO.setUserCreatorId(collectionLogDTO.getUserCreatorId());
            collectionLogVO.setUserCreatorImg(collectionLogDTO.getUserCreatorImg());
            collectionLogVO.setUserCreatorName(collectionLogDTO.getUserCreatorName());
            collectionLogVO.setFromUserName(collectionLogDTO.getUserName());
            collectionLogVO.setGiveTime(collectionLogDTO.getGiveTime());
            collectionLogVO.setGiveSuccessTime(collectionLogDTO.getGiveSuccessTime());
            vos.add(collectionLogVO);
        });
        resultPage.setRecords(vos);
        return PageResult.success(resultPage);
    }

    @ApiOperation(value = "未开启的盲盒", notes = "未开启的盲盒", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/notOpenBlindBox", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<List<NotOpenBlindBoxVO>> notOpenBlindBox() {

        return Result.success(userCollectionService.notOpenBlindBox(null));
    }

    @ApiOperation(value = "获取指定盲盒类型未开启的盲盒", notes = "获取指定盲盒类型未开启的盲盒", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/notOpenBlindBoxById", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<List<NotOpenBlindBoxVO>> notOpenBlindBoxById(@RequestBody IdRequest idRequest) {
        return Result.success(userCollectionService.notOpenBlindBox(idRequest.getId()));
    }

    @ApiOperation(value = "已拥有的特权藏品详情", notes = "已拥有的特权藏品详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/getPrivilegeCollectionDetail", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<HavingPrivilegeCollectionDetailVO> getPrivilegeCollectionDetail(@Valid @RequestBody IdRequest request) {
        HavingPrivilegeCollectionDetailVO havingPrivilegeCollectionDetailVO = userCollectionService.getPrivilegeCollectionDetail(request.getId());
        return Result.success(havingPrivilegeCollectionDetailVO);
    }

    @ApiOperation(value = "已拥有的藏品详情", notes = "已拥有的藏品详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/getCollectionDetail", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<HavingCollectionDetailVO> getCollectionDetail(@Valid @RequestBody IdRequest request) {
        HavingCollectionDetailVO havingCollectionDetailVO = userCollectionService.getCollectionDetail(UserPool.getUser(), request.getId());
        return Result.success(havingCollectionDetailVO);
    }
//    @ApiOperation(value = "初始化防囤", notes = "初始化防囤", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
//    @PostMapping(value = "/initAnti", produces = MediaType.APPLICATION_JSON_VALUE)
//    public Result initAnti() {
//        val list = digitalCollectionDao.list();
//        list.forEach(userCollectionService::initAnti);
//        return Result.success();
//    }

    @ApiOperation(value = "藏品链记录", notes = "藏品链记录", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/getCollectionChainRecord", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<CollectionChainRecord> getCollectionChainRecord(@Valid @RequestBody GetCollectionChainRecordRequest request) {
        return PageResult.success(userCollectionService.getCollectionChainRecord(UserPool.getUser().getId(), request.getType(), request.getPageNo(), request.getPageSize()));
    }

    @ApiOperation(value = "获取用户持有的藏品", notes = "获取用户持有的藏品", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/whetherTheUserOwnsACollection", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<WhetherTheUserOwnsACollection> whetherTheUserOwnsACollection(@Valid @RequestBody IdRequest request) {
        WhetherTheUserOwnsACollection whetherTheUserOwnsACollection = new WhetherTheUserOwnsACollection();
        whetherTheUserOwnsACollection.setIsPrivilege(userCollectionService.whetherTheUserOwnsACollection(UserPool.getUser().getId(), request.getId()));
        return Result.success(whetherTheUserOwnsACollection);
    }


    @ApiOperation(value = "用户已转让盲盒", notes = "用户已转让盲盒", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/myBoxTransfer", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<MyBoxTransferVO> myBoxTransfer(@RequestBody PageRequest page) {
        return PageResult.success(userCollectionService.myBoxTransfer(UserPool.getUser().getId(), page.getPageNo(), page.getPageSize()));
    }

    @ApiOperation(value = "用户已接收盲盒", notes = "用户已接收盲盒", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/myBoxReceive", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<MyBoxReceiveVO> myBoxReceive(@RequestBody PageRequest page) {
        return PageResult.success(userCollectionService.myBoxReceive(UserPool.getUser().getId(), page.getPageNo(), page.getPageSize()));
    }

    @ApiOperation(value = "盲盒分组", notes = "盲盒分组", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/boxGroupUser", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<BoxGroupVO> boxGroupUser(@RequestBody PageRequest page) {
        return PageResult.success(userCollectionService.boxGroupUser(UserPool.getUser().getId(), page.getPageNo(), page.getPageSize()));
    }


    @ApiOperation(value = "盲盒列表", notes = "盲盒列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/boxGroupList", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<BoxGroupListVO> boxGroupList(@RequestBody BoxGroupListDTO boxGroupListDTO) {
        return PageResult.success(userCollectionService.boxGroupList(boxGroupListDTO, UserPool.getUser()));
    }



    @ApiOperation(value = "批量转增", notes = "批量转增", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/batchGive", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result batchGive(@Valid @RequestBody GiveBatchDTO giveBatchDTO) {
        if (true) {
            throw new GlobalRunTimeException("该版本转赠功能已经关闭,请升级至最新版本");
        }
        List<RLock>locks=new ArrayList<>();
        UserInfo user = UserPool.getUser();
        String userAccout;
        if (user.getIsUser() == UserAccoutTypeEnum.USER_TEL.getCode()) {
            userAccout = user.getUserTel();
        } else {
            userAccout = user.getUserEmail();
        }
        FilterIncomeList isTeamIncomeByUserId = filterIncomeListDao.getIsTeamIncomeByUserId(user.getId());
        if (ObjectUtil.isNull(isTeamIncomeByUserId)) {
            checkParameterService.CheckVerificationcode(userAccout, giveBatchDTO.getSmsId(), giveBatchDTO.getCodes(), SendSMSEnum.TRANSFER.getCode());
        } else {
            if (!StrUtil.equals(user.getUserTel().substring(user.getUserTel().length() - 6), giveBatchDTO.getCodes())) {
                throw new GlobalRunTimeException(ErrorCode.VERIFICATION_ERROR);
            } else {
                user.setIsValid(true);//市值假装实名过
            }
        }
        // 所有的转增加锁
        try {
            for (Long l : giveBatchDTO.getList()) {
                RLock lock = redisson.getLock(StrUtil.format("{}:{}",RedisKeyEnum.TRANSFER_LOCK.getKey() , l));
                boolean res = lock.tryLock(3,  TimeUnit.SECONDS);
                if (res) {
                    locks.add(lock);
                }else {
                    throw new GlobalRunTimeException("藏品正在被转赠中，请稍后再试");
                }
            }
            userCollectionService.batchGive(UserPool.getUser(),giveBatchDTO);
        } catch (InterruptedException e) {
            throw new GlobalRunTimeException("系统繁忙，请稍后再试");
        } finally {
            locks.forEach(RLock::unlock);
        }

        return Result.success();
    }


    @ApiOperation(value = "我的版权藏品", notes = "我的版权藏品", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/myCopyrightCollectionNow", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<MyCopyrightCollectionNowVO> myCopyrightCollectionNow(@RequestBody PageRequest pageRequest) {
        return PageResult.success(userCollectionService.myCopyrightCollectionNow(UserPool.getUser().getId(), pageRequest.getPageNo(), pageRequest.getPageSize()));
    }


    @ApiOperation(value = "我的版权藏品详情", notes = "我的版权藏品详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/myCopyrightCollectionDetails", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<MyCopyrightCollectionDetailsVO> myCopyrightCollectionDetails(@RequestBody IdRequest idRequest) {
        return Result.success(userCollectionService.myCopyrightCollectionDetails(UserPool.getUser(), idRequest.getId()));
    }

}